﻿using commonImportPlugin.Configs.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.TrackFile
{
    public class TrackFileConfig :ConfigsBase, IcommonBase
    {
        //
        //    基本配置参数
        //
        #region
        /// <summary>
        /// 工厂配置
        /// </summary>
        public string factory { set; get; }
        /// <summary>
        /// 筛选数据文件规则
        /// (文件名正则[#]文件内容正则)
        /// </summary>
        public string fileFilterRules { set; get; }
        /// <summary>
        /// 编码格式
        /// </summary>
        public string encoding { set; get; }
        /// <summary>
        /// 磁条数据起始行（1开始）
        /// </summary>
        public int dataIndex { set; get; }
        /// <summary>
        /// 磁条分隔符
        /// </summary>
        public string[] dataSeparators { set; get; }
        /// <summary>
        /// 磁条中银行卡号获取规则
        /// </summary>
        public int[] subBankCardId { set; get; }
        /// <summary>
        /// 磁条中身份号获取规则
        /// </summary>
        public int[] subPersonId { set; get; }
        /// <summary>
        /// 获取磁条1规则
        /// </summary>
        public int[] subTrack1 { set; get; }
        /// <summary>
        /// 获取磁条1规则
        /// </summary>
        public int[] subTrack2 { set; get; }
        /// <summary>
        /// 获取磁条1规则
        /// </summary>
        public int[] subTrack3 { set; get; }
        /// <summary>
        /// 磁条数据检查
        /// </summary>
        public string dataCheckRegex { set; get; }
        /// <summary>
        /// 该参数用于实现过滤之前已导入的数据
        /// 该参数值为：bankCardId,personId,ssCardId之一
        /// </summary>
        public string filterItem { set; get; }
        #endregion

        //
        //    其他
        //
        #region
        private static TrackFileConfig uniqueInstance;

        /// <summary>
        /// 初始化配置
        /// </summary>
        /// <param name="configs"></param>
        public void Init()
        {
            TrackFileConfig config = new TrackFileConfig();

            //检查参数配置的参数格式为：数字(+)&数字(-+)&数字(-+)
            string checkRegexs = @"^\d{1,}&(-\d{1,}|\d{1,})&(-\d{1,}|\d{1,})$";           

            //获取参数
            #region
            //工厂名
            config.factory = configs["TrackFileFactory"];

            //扩展名
            if (!IsKeyExist("TrackFileFilterRules"))
                throw new Exception(TrackErrors.TrackFileFilterRulesError);

            config.fileFilterRules = configs["TrackFileFilterRules"];

            //编码方式
            if (!IsKeyEmpty("TrackFileEncoding"))
                config.encoding = configs["TrackFileEncoding"];
            else
                config.encoding = "GB2312";//默认值

            //数据起始行
            if (!IsKeyExist("TrackFileDataIndex"))
                throw new Exception(TrackErrors.TrackFileDataIndexError1);

            if (!Regex.IsMatch(configs["TrackFileDataIndex"], @"\d{1,}"))
                throw new Exception(TrackErrors.TrackFileDataIndexError2);

            config.dataIndex = int.Parse(configs["TrackFileDataIndex"]);

            //磁条分隔符
            if (!IsKeyExist("TrackFileDataSeparators"))
                throw new Exception(TrackErrors.TrackFileDataSeparatorsError);

            config.dataSeparators = ToStringArrays(configs["TrackFileDataSeparators"]);

            //获取银行卡号
            if (!IsKeyEmpty("TrackFileSubBankCardId"))
            {
                if (!Regex.IsMatch(configs["TrackFileSubBankCardId"], checkRegexs))
                    throw new Exception(TrackErrors.TrackFileSubBankCardIdError);

                config.subBankCardId = ToIntArrays(configs["TrackFileSubBankCardId"]);
            }

            //获取身份证号
            if (!IsKeyEmpty("TrackFileSubPersonId"))
            {
                if (!Regex.IsMatch(configs["TrackFileSubPersonId"], checkRegexs))
                    throw new Exception(TrackErrors.TrackFileSubPersonIdError);

                config.subPersonId = ToIntArrays(configs["TrackFileSubPersonId"]);
            }

            //获取1磁道
            if (!IsKeyEmpty("TrackFileSubTrack1"))
            {
                if (!Regex.IsMatch(configs["TrackFileSubTrack1"], checkRegexs))
                    throw new Exception(TrackErrors.TrackFileSubTrack1Error);

                config.subTrack1 = ToIntArrays(configs["TrackFileSubTrack1"]);
            }

            //获取2磁道
            if (!IsKeyEmpty("TrackFileSubTrack2"))
            {
                if (!Regex.IsMatch(configs["TrackFileSubTrack2"], checkRegexs))
                    throw new Exception(TrackErrors.TrackFileSubTrack2Error);

                config.subTrack2 = ToIntArrays(configs["TrackFileSubTrack2"]);
            }

            //获取3磁道
            if (!IsKeyEmpty("TrackFileSubTrack3"))
            {
                if (!Regex.IsMatch(configs["TrackFileSubTrack3"], checkRegexs))
                    throw new Exception(TrackErrors.TrackFileSubTrack3Error);

                config.subTrack3 = ToIntArrays(configs["TrackFileSubTrack3"]);
            }

            //检查磁条正则
            if (IsKeyExist("TrackFileDataCheckRegexs"))
            {
                config.dataCheckRegex = configs["TrackFileDataCheckRegexs"];
            }

            //获取过滤数据项
            if (!IsKeyEmpty("TrackFileFilterItem"))
            {
                string[] items = new string[] { "bankCardId", "personId", "ssCardId" };
                string item = configs["TrackFileFilterItem"].Trim();

                if (!items.Contains(item))
                    throw new Exception(TrackErrors.TrackFileFilterItemError);

                config.filterItem = configs["TrackFileFilterItem"];
            }

            #endregion


            uniqueInstance= config;
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static TrackFileConfig GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    }
}
